package com.shanghai.shanghaielectric.UI.setting;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.shanghai.shanghaielectric.Constants;
//import com.shanghai.shanghaielectric.DB.SharepreferenceUtil;
import com.shanghai.shanghaielectric.R;
import com.shanghai.shanghaielectric.UI.DilaogView.Dialog_Alert_Control_Command;

import shanghaielectric_lib.Utils.dwin.Dwin;


/**
 * 设置-通讯界面
 */
@SuppressLint("NewApi")
public class SettingCommunicationFragment extends Fragment {
    Dialog_Alert_Control_Command.Alert_Command_Listener alert_command_listener_comuunicate = new Dialog_Alert_Control_Command.Alert_Command_Listener() {
        @Override
        public void getBack(boolean isChooseCertain, String commandName) {
            if (isChooseCertain){
                Toast.makeText(mContext, "系统重启中...", Toast.LENGTH_LONG).show();
                Dwin.reboot();
            }
        }
    };
    String[] array_speed = new String[]{"9600", "19200"};
    ArrayAdapter adapt_speed;
    public View settingCommunicationView;
    Spinner setting_Speed;
    private EditText setting_DevNum, setting_DataBits, setting_StopBits, setting_Parity;
    private RadioGroup setting_controlport;
    private static SharedPreferences psdPreferences;
    private RadioButton setting_radio_controlport1, setting_radio_controlport2;
    private Context mContext = null;
    private int pause = 0;
    Button reboot_app;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        System.out.println("SettingCommunication____onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("SettingCommunication____onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("SettingCommunication____onCreateView");
        settingCommunicationView = inflater.inflate(R.layout.setting_communication_fragment, container, false);
        mContext = getActivity().getApplicationContext();
        psdPreferences = mContext.getSharedPreferences("psd", mContext.MODE_PRIVATE);
        pause = 0;
        initView();

        initListener();
        return settingCommunicationView;
    }

    public void initView() {

        reboot_app = (Button) settingCommunicationView.findViewById(R.id.reboot_app);

        setting_DevNum = (EditText) settingCommunicationView.findViewById(R.id.setting_DevNum);
        setting_Speed = (Spinner) settingCommunicationView.findViewById(R.id.setting_Speed);
        setting_DataBits = (EditText) settingCommunicationView.findViewById(R.id.setting_DataBits);
        setting_StopBits = (EditText) settingCommunicationView.findViewById(R.id.setting_StopBits);
        setting_Parity = (EditText) settingCommunicationView.findViewById(R.id.setting_Parity);


        setting_DevNum.setText(Constants.DevNum + "");
        setting_DataBits.setText(Constants.DataBits + "");
        setting_StopBits.setText(Constants.StopBits + "");
        setting_Parity.setText(Constants.Parity + "");


        adapt_speed = new ArrayAdapter<String>(mContext, R.layout.spinner_item, array_speed);
        adapt_speed.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //绑定 Adapter到控件
        setting_Speed.setAdapter(adapt_speed);
//        int speed = (int) SharepreferenceUtil.get(mContext, "speed", Constants.DefaultSpeed);
//        if (speed == Constants.DefaultSpeed) {
//            setting_Speed.setSelection(0, true);
//        } else if (speed == Constants.Speed_19200) {
//            setting_Speed.setSelection(1, true);
//        }
    }


    public void initListener() {

        // setting_save.setOnClickListener(onClickListener);
        // setting_cancle.setOnClickListener(onClickListener);
        setting_Speed.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                SharepreferenceUtil.putAndApply(mContext, "speed", Integer.parseInt(array_speed[i]));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        reboot_app.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
//                MyApplication.exit();

//                Intent i =mContext.getApplicationContext().getPackageManager().getLaunchIntentForPackage(mContext.getPackageName());
//                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(i);
                showDiolg("resetAPP");

            }
        });
        Dialog_Alert_Control_Command.setAlert_resetSystem_Listener(alert_command_listener_comuunicate);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("SettingCommunication____onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("SettingCommunication____onStart");
    }

    @Override
    public void onResume() {
        super.onResume();

        System.out.println("SettingCommunication____onResume");
    }

    @Override
    public void onPause() {
        super.onPause();

        pause = 1;
        System.out.println("SettingCommunication____onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("SettingCommunication____onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("SettingCommunication____onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("SettingCommunication____onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        System.out.println("SettingCommunication____onDetach");
    }

    private void showDiolg(String commandName) {
        Dialog_Alert_Control_Command dialog_Alert_Control_Command = new Dialog_Alert_Control_Command();
        dialog_Alert_Control_Command.setCommandName(commandName);
        dialog_Alert_Control_Command.show(this.getFragmentManager(), "");

    }
}
